package net.example.utils;

import com.oraycn.esbasic.helpers.StringHelper;
import com.oraycn.esframework.common.TransferingProject;
import com.oraycn.esframeworkboost.networkdisk.Comment4NDisk;
import com.oraycn.esframeworkboost.networkdisk.NDiskParameters;
import net.example.model.FileTransferUseType;
import net.example.model.GGGroup;
import net.example.model.GGUser;
import net.example.model.UserBasicModel;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CommonHelper {
    //获取最大显示数量字符串
    public static String getMaxCountStr4Show(int count)
    {
        if(count>99)
        {
            return "99+";
        }
        return String.valueOf(count);
    }

    /// <summary>
    /// 检测是否为标准密码  (数字加英文，6-16位字符)
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static boolean isStandardPassword(String password)
    {
        if (StringHelper.isNullOrEmpty(password))
        {
            return false;
        }
        Pattern pattern=Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,18}$");
        Matcher matcher= pattern.matcher(password);
        return matcher.matches();
    }

    //获取OMCS登录真实账号
    public static String getUserID4OMCS(String id)
    {
        if(StringHelper.isNullOrEmpty(id)){return "";}
        if(!id.contains("#")){return id;}
        String[] strs= id.split("#");
        if(strs==null||strs.length==0){return "";}
        else if(strs.length==1){return strs[0];}
        return strs[1];
    }

    public static Collection<UserBasicModel> ggUserListConvertToUserBasicModel(Collection<GGUser> userList)
    {
        List<UserBasicModel> models=new ArrayList<UserBasicModel>();
        for (GGUser GGUser :userList)
        {
            models.add(new UserBasicModel(GGUser.getUserID(), GGUser.getName(), GGUser.getCommentName()));
        }
        return models;
    }

    public static Collection<UserBasicModel> ggGroupListConvertToUserBasicModel(Collection<GGGroup> GGGroupList)
    {
        List<UserBasicModel> models=new ArrayList<UserBasicModel>();
        for (GGGroup GGGroup : GGGroupList)
        {
            models.add(new UserBasicModel(GGGroup.getGroupID(), GGGroup.getName(),true));
        }
        return models;
    }

    /**
     * 获取文件传输使用的类型
     * @param project
     * @return
     */
    public static FileTransferUseType getFileTransferUseType(TransferingProject project)
    {
        if(project==null)
        {
            return FileTransferUseType.Normal;
        }
        NDiskParameters nDiskParameters= Comment4NDisk.Parse(project.getComment());
        if(nDiskParameters!=null)
        {
            return FileTransferUseType.Comment4NDisk;
        }
        String offlineFileSenderID=Comment4OfflineFile.ParseUserID(project.getComment());
        if(!StringHelper.isNullOrEmpty(offlineFileSenderID))
        {
            return FileTransferUseType.OfflineFile;
        }
        return FileTransferUseType.Normal;
    }
}
